home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / jockey / xorg_driver.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  10.2 KB  |  296 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Abstract handler for a restricted X.org graphics driver.'''
  5. import os.path as os
  6. import logging
  7. import XKit.xutils as XKit
  8. import XKit.xorgparser as XKit
  9. from handlers import KernelModuleHandler
  10. from oslib import OSLib
  11.  
  12. def _(x):
  13.     return x
  14.  
  15.  
  16. class XorgDriverHandler(KernelModuleHandler):
  17.     '''Abstract class for a restricted X.org graphics driver.'''
  18.     
  19.     def __init__(self, backend, module, driver_package, xorg_driver, alt_free_driver, extra_conf_options = { }, add_modules = [], disable_modules = [], remove_modules = [], name = None, description = None, rationale = None):
  20.         '''Create handler for a particular X.org graphics driver.
  21.         
  22.         This usually consists of a kernel module and a driver package, plus
  23.         some xorg.conf configuration.
  24.  
  25.         - backend, module, driver_package, name, description, rationale: as in
  26.           KernelModuleHandler
  27.         - xorg_driver: name of the X.org driver as it appears in xorg.conf
  28.         - alt_free_driver: free fallback driver if this one gets disabled
  29.         - extra_conf_options: dictionary of extra "Option"s that go into the
  30.           Device section of xorg.conf
  31.         - add_modules: list of modules that get added to the "Module" section
  32.           of xorg.conf when enabling this driver (and removed when disabling)
  33.         - remove_modules: list of modules that get removed from the "Module"
  34.           section of xorg.conf when enabling this driver (and added when disabling)
  35.         '''
  36.         KernelModuleHandler.__init__(self, backend, module, name, description, rationale)
  37.         self.package = driver_package
  38.         self.xorg_driver = xorg_driver
  39.         self.alt_free_driver = alt_free_driver
  40.         self.extra_conf_options = extra_conf_options
  41.         self.add_modules = add_modules
  42.         self.disable_modules = disable_modules
  43.         self.remove_modules = remove_modules
  44.         if os.path.exists(OSLib.inst.xorg_conf_path):
  45.             
  46.             try:
  47.                 self.xorg_conf = XKit.xutils.XUtils(OSLib.inst.xorg_conf_path)
  48.             except XKit.xorgparser.ParseException:
  49.                 self.xorg_conf = None
  50.             except:
  51.                 None<EXCEPTION MATCH>XKit.xorgparser.ParseException
  52.             
  53.  
  54.         None<EXCEPTION MATCH>XKit.xorgparser.ParseException
  55.         self.xorg_conf = XKit.xutils.XUtils()
  56.  
  57.     
  58.     def id(self):
  59.         '''Return a unique identifier of the handler.'''
  60.         i = 'xorg:' + self.module
  61.         if self.driver_vendor:
  62.             i += ':' + self.driver_vendor.replace(' ', '_')
  63.         
  64.         return i
  65.  
  66.     
  67.     def can_change(self):
  68.         if self.xorg_conf:
  69.             return None
  70.         return _('Reconfiguring X.org video drivers is not possible: %s is invalid.') % OSLib.inst.xorg_conf_path
  71.  
  72.     
  73.     def enabled(self):
  74.         if self.xorg_conf:
  75.             
  76.             try:
  77.                 driver = self.xorg_conf.getDriver('Device', 0)
  78.             except (XKit.xorgparser.OptionException, XKit.xorgparser.SectionException):
  79.                 error = None
  80.                 driver = None
  81.                 logging.debug('XorgDriverHandler.enabled() [module: %s, package: %s, xorg driver: %s): exception %s, considering disabled', self.module, self.package, self.xorg_driver, str(error))
  82.                 logging.debug('%s', str(self.xorg_conf.globaldict))
  83.                 return False
  84.  
  85.             if self.xorg_conf.isDriverEnabled(self.xorg_driver) == False:
  86.                 driver = None
  87.                 logging.debug('XorgDriverHandler isDriverEnabled (%s, %s, %s): xorg.conf driver %s, considering disabled' % (self.module, self.package, self.xorg_driver, str(driver)))
  88.                 return False
  89.             logging.debug('XorgDriverHandler(%s, %s, %s): xorg.conf driver matches' % (self.module, self.package, self.xorg_driver))
  90.         
  91.         return KernelModuleHandler.enabled(self)
  92.  
  93.     
  94.     def used(self):
  95.         '''Return if the handler is currently in use.'''
  96.         if self.changed() and self.enabled():
  97.             return False
  98.         return KernelModuleHandler.used(self)
  99.  
  100.     
  101.     def _mod_status(self, module, action):
  102.         '''Check if a module is handled in current configuration.
  103.  
  104.         action can be "Load" or "Disable".
  105.         '''
  106.         for i in xrange(len(self.xorg_conf.globaldict['Module'])):
  107.             
  108.             try:
  109.                 modules = self.xorg_conf.getValue('Module', action, i)
  110.             except XKit.xorgparser.OptionException:
  111.                 continue
  112.  
  113.             if type(modules) == type(''):
  114.                 if modules == module:
  115.                     return True
  116.                 continue
  117.             modules == module
  118.             for mod in modules:
  119.                 if mod == module:
  120.                     return True
  121.             
  122.         
  123.         return False
  124.  
  125.     
  126.     def _mod_enabled(self, module):
  127.         return self._mod_status(module, 'Load')
  128.  
  129.     
  130.     def _mod_disabled(self, module):
  131.         return self._mod_status(module, 'Disable')
  132.  
  133.     
  134.     def enable(self):
  135.         if not self.xorg_conf:
  136.             logging.error('XorgDriverHandler.enable(): invalid xorg.conf, skipping')
  137.             return False
  138.         KernelModuleHandler.enable(self)
  139.         if not OSLib.inst.package_installed(self.package):
  140.             return None
  141.         has_proprietary = False
  142.         it = 0
  143.         for section in self.xorg_conf.globaldict['Device']:
  144.             
  145.             try:
  146.                 driver = self.xorg_conf.getDriver('Device', it)
  147.                 if driver == self.xorg_driver:
  148.                     has_proprietary = True
  149.                     break
  150.             except XKit.xorgparser.OptionException:
  151.                 OSLib.inst.package_installed(self.package)
  152.                 OSLib.inst.package_installed(self.package)
  153.                 self.xorg_conf
  154.             except:
  155.                 OSLib.inst.package_installed(self.package)
  156.  
  157.             it += 1
  158.         
  159.         relevant_devices = []
  160.         if len(self.xorg_conf.globaldict['Device']) == 0:
  161.             device = self.xorg_conf.makeSection('Device', identifier = 'Default Device')
  162.             relevant_devices.append(device)
  163.             self.xorg_conf.setDriver('Device', self.xorg_driver, device)
  164.         else:
  165.             devices = self.xorg_conf.getDevicesInUse()
  166.             if len(devices) > 0:
  167.                 relevant_devices = devices
  168.             else:
  169.                 relevant_devices = self.xorg_conf.globaldict['Device'].keys()
  170.             for device in relevant_devices:
  171.                 self.xorg_conf.setDriver('Device', self.xorg_driver, device)
  172.             
  173.         logging.debug('XorgDriverHandler device sections (%s)' % str(self.xorg_conf.globaldict['Device']))
  174.         module_sections = self.xorg_conf.globaldict['Module']
  175.         have_modules = len(module_sections) > 0
  176.         if have_modules:
  177.             for section in module_sections:
  178.                 for mod in self.remove_modules:
  179.                     self.xorg_conf.removeOption('Module', 'Load', value = mod, position = section)
  180.                 
  181.             
  182.         
  183.         if self.add_modules:
  184.             if not have_modules:
  185.                 module_section = self.xorg_conf.makeSection('Module')
  186.             else:
  187.                 module_section = 0
  188.             for mod in self.add_modules:
  189.                 if not self._mod_enabled(mod):
  190.                     self.xorg_conf.addOption('Module', 'Load', mod, optiontype = None, position = module_section, reference = True)
  191.                     continue
  192.             
  193.         
  194.         if self.disable_modules:
  195.             for mod in self.disable_modules:
  196.                 if not self._mod_disabled(mod):
  197.                     self.xorg_conf.addOption('Module', 'Disable', mod, optiontype = None, position = module_section, reference = True)
  198.                     continue
  199.             
  200.         
  201.         for device_section in relevant_devices:
  202.             for k, v in self.extra_conf_options.iteritems():
  203.                 self.xorg_conf.addOption('Device', k, v, optiontype = 'Option', position = device_section)
  204.             
  205.         
  206.         self.enable_config_hook()
  207.         self.xorg_conf.writeFile(OSLib.inst.xorg_conf_path)
  208.         return False
  209.  
  210.     
  211.     def disable(self):
  212.         if not self.xorg_conf:
  213.             logging.error('XorgDriverHandler.enable(): invalid xorg.conf, skipping')
  214.             return False
  215.         KernelModuleHandler.disable(self)
  216.         if OSLib.inst.package_installed(self.package):
  217.             return None
  218.         noconf = os.path.join(OSLib.inst.backup_dir, self.xorg_driver + '.noconf')
  219.         oldconf = os.path.join(OSLib.inst.backup_dir, self.xorg_driver + '.oldconf')
  220.         if os.path.exists(noconf):
  221.             logging.debug('XorgDriverHandler.disable(%s): previously had no xorg.conf, deleting xorg.conf', self.xorg_driver)
  222.             os.unlink(noconf)
  223.             self.xorg_conf = XKit.xutils.XUtils()
  224.         elif os.path.exists(oldconf):
  225.             logging.debug('XorgDriverHandler.disable(%s): restoring xorg.conf backup', self.xorg_driver)
  226.             open(OSLib.inst.xorg_conf_path, 'w').write(open(oldconf).read())
  227.             os.unlink(oldconf)
  228.             self.xorg_conf = XKit.xutils.XUtils(OSLib.inst.xorg_conf_path)
  229.         else:
  230.             logging.debug('XorgDriverHandler.disable(%s): missing xorg.conf, reversing changes from enable()', self.xorg_driver)
  231.             relevant_devices = []
  232.             if len(self.xorg_conf.globaldict['Device']) > 0:
  233.                 devices = self.xorg_conf.getDevicesInUse()
  234.                 if len(devices) > 0:
  235.                     relevant_devices = devices
  236.                 else:
  237.                     relevant_devices.append(0)
  238.                 module_sections = self.xorg_conf.globaldict['Module']
  239.                 have_modules = len(module_sections) > 0
  240.                 if have_modules:
  241.                     for section in module_sections:
  242.                         for mod in self.add_modules:
  243.                             self.xorg_conf.removeOption('Module', 'Load', value = mod, position = section)
  244.                         
  245.                         for mod in self.disable_modules:
  246.                             self.xorg_conf.removeOption('Module', 'Disable', value = mod, position = section)
  247.                         
  248.                     
  249.                 
  250.                 if self.remove_modules:
  251.                     if not have_modules:
  252.                         module_section = self.xorg_conf.makeSection('Module')
  253.                     else:
  254.                         module_section = 0
  255.                     for mod in self.remove_modules:
  256.                         if not self._mod_enabled(mod):
  257.                             self.xorg_conf.addOption('Module', 'Load', mod, optiontype = None, position = module_section, reference = True)
  258.                             continue
  259.                     
  260.                 
  261.                 for device_section in relevant_devices:
  262.                     self.xorg_conf.setDriver('Device', self.alt_free_driver, device_section)
  263.                     for k, v in self.extra_conf_options.iteritems():
  264.                         self.xorg_conf.removeOption('Device', k, v, position = device_section)
  265.                     
  266.                 
  267.             
  268.             self.disable_config_hook()
  269.             logging.debug('XorgDriverHandler.disable(%s): writing restored xorg.conf', self.xorg_driver)
  270.             self.xorg_conf.writeFile(OSLib.inst.xorg_conf_path)
  271.         return False
  272.  
  273.     
  274.     def enable_config_hook(self):
  275.         '''Custom self.xorg_config changes after driver, modules, and extra
  276.         driver options have been changed.
  277.         '''
  278.         pass
  279.  
  280.     
  281.     def disable_config_hook(self):
  282.         '''Custom self.xorg_config changes after driver, modules, and extra
  283.         driver options have been changed.
  284.         '''
  285.         pass
  286.  
  287.     
  288.     def enables_composite(self):
  289.         '''Return whether this driver enables the composite extension.
  290.         
  291.         Note that this is true if and only if the current driver does *not*
  292.         support composite, but this driver does.'''
  293.         return False
  294.  
  295.  
  296.